package idbuilder

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"sync/atomic"
)

const (
	imageObjectIDTemplate = "%s%s%s%05d" //生成ImageObjectID的模板
	//模板顺序为设备编码 + 子类型编码 + 时间编码 + 流水序号
)

type VideoSubType string

const (
	VideoSubTypeVideo VideoSubType = "01" //视频片段
	VideoSubTypeImage VideoSubType = "02" //图像
	VideoSubTypeFile  VideoSubType = "03" //文件
	VideoSubTypeOther VideoSubType = "99" //其他
)

var (
	ImageObjectIDCounter uint32 //流水序号
)

func getImageObjectIDCounter() uint32 {
	return atomic.AddUint32(&ImageObjectIDCounter, 1)
}

func GetImageObjectID(deviceID string, subType VideoSubType) string {
	if len(deviceID) != 20 {
		log.Warnln("非法设备ID:", deviceID)
	}
	return fmt.Sprintf(imageObjectIDTemplate, deviceID, subType, GetNowTimeStr(), getImageObjectIDCounter()%100000)
}

func GetImageObjectIDByTimeStr(timeStr, deviceID string, subType VideoSubType) string {
	if len(deviceID) != 20 {
		log.Warnln("非法设备ID:", deviceID)
	}
	return fmt.Sprintf(imageObjectIDTemplate, deviceID, subType, GetNowTimeStr(), getImageObjectIDCounter()%100000)
}
